/** @file    FormThreadManager.cpp
 *  @author  Alessandro Polo
 *  @version $Id: FormThreadManager.cpp 2867 2010-08-07 20:44:51Z alex $
 *  @brief
 * File containing methods for the wosh::gui::FormThreadManager class.
 * The header for this class can be found in FormThreadManager.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include "FormThreadManager.h"
 #include "ui_FormThreadManager.h"
 #include <core/ThreadManager.h>

 #include <QtGui/QMessageBox>


using namespace std;
namespace wosh {
 namespace gui {

 WOSH_REGISTER(wosh::gui::FormThreadManager, "wosh::gui::IWidgetGeneric", 2.01, _static_FormThreadManager )
 WOSH_GUI_MAP(wosh::gui::FormThreadManager, "wosh::ThreadManager", 2.00, _static_Map_FormThreadManager )

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

FormThreadManager::FormThreadManager(QWidget *parent)
	: IWidgetGeneric(parent) {
	this->m_ui = new Ui::FormThreadManager();
	this->m_ui->setupUi(this);

	IWidgetGeneric::setTargetURI(_ThreadManager_URI);

	// tab Threads [0]
	this->m_ui->tableView->setResizeStretch(1);
	this->m_ui->tableView->getTableView()->setContextMenuPolicy(Qt::CustomContextMenu);
	connect(this->m_ui->tableView, SIGNAL(postMessage(Message*)),this, SLOT(postMessage(Message*)) );
	connect(this->m_ui->btnThreadStart, SIGNAL(clicked()), this, SLOT(btnThreadStart_clicked()));
	connect(this->m_ui->btnThreadStop, SIGNAL(clicked()), this, SLOT(btnThreadStop_clicked()));
	connect(this->m_ui->btnThreadTerminate, SIGNAL(clicked()), this, SLOT(btnThreadTerminate_clicked()));

	// tab Configuration [1]
	this->propMapperCfg = new PropertiesMapper(this, true);
	connect(this->propMapperCfg, SIGNAL(send_request(const QString&, ISerializable*)), this, SLOT(executeRemoteMethod(const QString&, ISerializable*)));
	this->propMapperCfg->set(_ThreadManager_KEY_ThreadMonitor, this->m_ui->chkThreadMonitor);
	connect(this->m_ui->wUpBarConfiguration, SIGNAL(updateClicked()), this, SLOT(refreshConfiguration()));

 }

FormThreadManager::~FormThreadManager() {
	delete this->m_ui;
 }

void FormThreadManager::changeEvent(QEvent *e) {
	QWidget::changeEvent(e);
	if ( e->type() == QEvent::LanguageChange )
		this->m_ui->retranslateUi(this);
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormThreadManager::refreshAll() {
	this->setWindowTitle( QString("Thread Manager [%1]").arg(this->getTargetURI().getKernelName().c_str()) );
	this->m_ui->tableView->refreshData();
	refreshConfiguration();
 }
void FormThreadManager::refreshConfiguration() {
	List* dlProps = this->propMapperCfg->createKeysList();
	executeRemoteMethod( _METHOD_GetProperties, dlProps );
	this->m_ui->wUpBarConfiguration->sayRetrieving();
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormThreadManager::btnThreadStart_clicked() {
	if ( !GuiUtilities::warning(tr("(Forced) Thread Operation."), tr("This operation is deprecated and for expert users only. Continue?") ) )
		return;
	Variant threadID = this->m_ui->tableView->getCurrentItemAsVariantAtCol(0);
	executeRemoteMethod( _ThreadManager_METHOD_start, threadID.clone() );
 }

void FormThreadManager::btnThreadStop_clicked() {
	if ( !GuiUtilities::warning(tr("(Forced) Thread Operation."), tr("This operation is deprecated and for expert users only. Continue?") ) )
		return;
	Variant threadID = this->m_ui->tableView->getCurrentItemAsVariantAtCol(0);
	executeRemoteMethod( _ThreadManager_METHOD_stop, threadID.clone() );
 }

void FormThreadManager::btnThreadTerminate_clicked() {
	if ( !GuiUtilities::warning(tr("(Forced) Thread Operation."), tr("This operation is deprecated and for expert users only. Continue?") ) )
		return;
	Variant threadID = this->m_ui->tableView->getCurrentItemAsVariantAtCol(0);
	executeRemoteMethod( _ThreadManager_METHOD_terminate, threadID.clone() );
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormThreadManager::busMessage( const Message& message, const Bus* ) {
	if ( message.isEmpty() || !message.getContent()->isResponse() ) return;
	if ( !isMessageForMeOrFromTarget(message) ) return;
	const wosh::Response* response = message.getContent()->asResponse();

	this->m_ui->tableView->evalResponse(response);

	if ( this->propMapperCfg->evalResponse(response) )
		this->m_ui->wUpBarConfiguration->sayReady();

	if ( response->getMethod() == _ThreadManager_METHOD_start ) {
		if ( WSUCCEEDED(response->getReturnValue()) ) {
			QMessageBox::information(this, tr("Thread Manager"), tr("SUCCEDDED Starting Thread!") );
			refreshAll();
		 }
		else
			QMessageBox::information(this, tr("Thread Manager"), QString("FAILED Starting Thread!") );
	 }
	else if ( response->getMethod() == _ThreadManager_METHOD_stop ) {
		if ( WSUCCEEDED(response->getReturnValue()) ) {
			QMessageBox::information(this, tr("Thread Manager"), tr("SUCCEDDED Stopping Thread!") );
			refreshAll();
		 }
		else
			QMessageBox::information(this, tr("Thread Manager"), QString("FAILED Stopping Thread!") );
	 }
	else
		promptMethodResponse(message);
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 }; // namespace gui
}; // namespace wosh
